package org.wii.jdbc.general.plugins;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author tanghong on 2022/12/23
 */
@Configuration
@ConditionalOnMissingBean(MybatisPlusInterceptor.class)
public class MybatisPluginConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        if (!CollectionUtils.isEmpty(getInterceptors())){
            getInterceptors().forEach(mybatisPlusInterceptor::addInnerInterceptor);
        }
        return mybatisPlusInterceptor;
    }

    /**
     * 获取配置拦截器列表(可重写扩展需要加载拦截器)
     * @return 拦截器列表
     */
    public List<InnerInterceptor> getInterceptors(){
        return Collections.emptyList();
    }
}
